package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/*
* 登录页面
* */
public class LoginPage extends Utils {

    public static String url = "http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(url);
    }

    /*
    * 检查页面是否加载成功
    * */
    public void checkPageRight(){
        //检查菜单是否加载成功
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    /*
    * 登录异常
    * ---用户名和密码都为空
    * ---用户名为空，密码不为空
    * ---用户名不为空，密码为空
    * ---输入账号正确，密码错误
    * ---输入账号错误(不存在)，密码正确
    * ---输入账号错误，密码错误
    * ---输入框：输入过长、过短、特殊字符......
    * */

    //输入正确的账号，错误的密码
    public void LoginFail(){
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("070124");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理弹窗 —— 警告弹窗
        //隐式等待不能作用于弹窗!
        //显示等待
        //等待弹窗加载完成
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();

        alert.accept();

        String expectMsg = "密码错误";

        assert actualMsg.equals(expectMsg);
    }

    //输入正确的账号，正确的密码
    public void LoginSuc(){
        //先清空上次的输入内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //登录成功会进入到列表页
        //检查是否登录成功 —— 检查是否有注销菜单
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
    }
}
